/*
** plsp.h for  in /home/lucas/projet/philosophe/laidfilauxzauphe
** 
** Made by Lucas Stefas
** Login   <stefas_l@epitech.net>
** 
** Started on  Sat Apr 30 10:06:47 2011 Lucas Stefas
** Last update Sat Apr 30 10:06:48 2011 Lucas Stefas
*/

#ifndef __LAIDFILAUXZAUPHE_PLSP_H__
#define __LAIDFILAUXZAUPHE_PLSP_H__

#include <time.h>
#include <stdio.h>
#include <pthread.h>

#define TIME_DEATH	10
#define TIME_EAT	1

typedef struct	s_lifo
{
  char		*str;
  struct s_lifo	*next;
}		lifo;

typedef struct	s_nbtruc
{
  size_t	nbbaguette;
  size_t	nbphilosophe;
}		nbtruc;

typedef struct s_baguette
{
  size_t		id;
  pthread_mutex_t	mutex;
  int			is_used;
  int			(*be_used)(struct s_baguette *);
  void			(*free)(struct s_baguette *);
} baguette;

typedef struct s_table
{
  size_t		nb_baguettes;
  baguette**		baguettes;
  pthread_mutex_t	starter;
  pthread_mutex_t	looker;
} table;


typedef struct s_philosophe
{
  table*		table;
  pthread_t		thread;
  size_t		id;
  baguette*		left_hand;
  baguette*		right_hand;
  struct timespec	start;
  baguette*		(*look_for_a_baguette)(struct s_philosophe* self);
  void			(*add_baguette)(struct s_philosophe*, baguette*);
  void			(*remove_baguette)(struct s_philosophe*);
  void			(*think)(struct s_philosophe*);
  void			(*eat)(struct s_philosophe*);
  void			(*sleep)(struct s_philosophe*);
} philosophe;

/*		constructors.c		*/
philosophe*	new_philosophe(size_t id, table *table);
baguette*	new_baguette(size_t id);
table*		new_table(baguette** baguettes, size_t nb_baguettes);

/*		baguette.c		*/
int		be_used(baguette* self);
void		free_baguette(baguette *self);

/*		philosophe.c		*/
void		think(philosophe * self);
void		eat(philosophe * self);
baguette*	look_for_a_baguette(philosophe* self);
void		has_free_baguette(philosophe* self, size_t n);
//void		is_alive(philosophe *self);
/*		philosophe.c		*/
void		dodo(philosophe * self);
void		add_baguette(philosophe *self, baguette *baguettes);
void		remove_baguette(philosophe * self);
void		will_do(philosophe *self);
void*		run_philosophe(void *arg);

/*		list.c			*/
lifo		*addinlist(lifo *list, char *str);
lifo		*popelem(lifo *list);

/*		parseur_xml2.c		*/
char		*epurstr(char *str);
int		findouverture(char *str, int *start);
int		findfermeture(char *str, int *end);
char		*getbalisename(char *str, int namelen);
int		my_strcmp(char *str1, char *str2);

/*		parseur_xml.c		*/
int		findendbalise(char *balise, char *name, int namelen);
lifo		*getaline(char *str, lifo *list, size_t *res);
lifo		*readfile(FILE *fichier, lifo *list, nbtruc *truc);
nbtruc		parseur(char *str);
void		printmyerror(int a, int b);

#endif
